* Cohesive Institutions and Political Violence
* Thiemo Fetzer and Stephan Kyburz

set graphics off

*****
*****
**
** SECTION: Produces summary figures
**
*****
*****


egen collapselgaalloc = sum(lgc_alloc_mo ), by(tm)

egen collapsegsaalloc = sum(lgc_gsa_mo ), by(tm)
egen collapsextraalloc = sum(lgc_extra_alloc_mo  ), by(tm)

egen meanfittedlgc_elec_status  = mean(fittedlgc_elec_status ), by(tm)
gen tm2 = tm
format tm2 %tm

twoway (line meanfittedlgc_elec_status  tm2 if tm> 473 & tm <655 &  ID_lgc== 810  ,  ytitle("Share LGC with elected", axis(1))) ,   scheme(s1color) xtitle("Time")  legend( region(lwidth(none)) order (1 2 ) label(1 "Share of LGAs with elected") label(2 "LGA Revenue Allocations") cols(2))
graph export "figures/elected.png", replace

* in greyscales
twoway (line meanfittedlgc_elec_status  tm2 if tm> 473 & tm <655 &  ID_lgc== 810  , lcolor(gs5) ytitle("Share LGC with elected", axis(1))) ,   scheme(s1color) xtitle("Time")  legend( region(lwidth(none)) order (1 2 ) label(1 "Share of LGAs with elected") label(2 "LGA Revenue Allocations") cols(2))
graph export "figures/elected_BW.png", replace


twoway (connected lgc_elec_status tm2 if ID_lgc==1 & tm<=508, col(dknavy) msize(.5)) (connected lgc_elec_status tm2 if ID_lgc==1 &tm>508 & tm<=532,  col(maroon) msize(.5)) (connected lgc_elec_status tm2 if ID_lgc==1 &tm>532 & tm<=556, col(dknavy) msize(.5))  (connected lgc_elec_status tm2 if ID_lgc==1 &tm>556 & tm<=575, col(maroon) msize(.5))    (connected lgc_elec_status tm2 if ID_lgc==1 &tm>575 & tm<=599, col(dknavy) msize(.5))   (connected lgc_elec_status tm2 if ID_lgc==1 &tm>599 & tm<=659, col(maroon) msize(.5)), xline(468 533 576)  legend(off) scheme(s1color) xtitle("Time")
graph export "figures/elected-appointed-ID_lgc1.png", replace


twoway (connected lgc_elec_status tm2 if ID_lgc==282 & tm<=508, col(dknavy) msize(.5)) (connected lgc_elec_status tm2 if ID_lgc==282 &tm>508 & tm<=529,  col(maroon) msize(.5)) (connected lgc_elec_status tm2 if ID_lgc==282 &tm>529 & tm<=570, col(dknavy) msize(.5))  (connected lgc_elec_status tm2 if ID_lgc==282 &tm>570 & tm<=575, col(maroon) msize(.5))    (connected lgc_elec_status tm2 if ID_lgc==282 &tm>575  , col(dknavy) msize(.5)) , xline(468 530 576 600 624 648)  legend(off) scheme(s1color) xtitle("Time")
graph export "figures/elected-appointed-ID_lgc282.png", replace

sort tm
* in greyscales
twoway (line brent tm2 if ID_lgc== 810 & tm <655, lcolor(gs8)  ytitle("Brent price in USD", axis(1))) (line collapsegsaalloc  tm2 if tm> 473 & tm <655 &  ID_lgc== 810   ,  lcolor(gs2) ytitle("LGA allocations in Mio USD", axis(2)) yaxis(2)) ,  scheme(s1color)  xtitle("Time")  legend(region(lwidth(none)) order (1 2 ) label(1 "Brent Oil price") label(2 "LGA Statutory Allocations") cols(2))
graph export "figures/statutoryallocations-oilprice.png", replace

twoway (line brent tm2 if ID_lgc== 810 ,  ytitle("Brent price in USD", axis(1))) (line collapselgaalloc tm2 if tm> 473 & tm <655 &  ID_lgc== 810   ,  ytitle("LGA allocations in Mio USD", axis(2)) yaxis(2)) ,  scheme(s1color)  xtitle("Time")  legend( region(lwidth(none)) order (1 2 ) label(1 "Brent Oil price") label(2 "LGA Revenue Allocations") cols(2))
*graph export "Draft/figures/allocations-oilprice.png", replace


global anywidelayout = "anyevents  anybattle anycivilianviolence anyprotestevents  anymilitaryevents anypoliticalmilitiaevents anycommunalmilitiaevents anyrebelsevents anymilitaryvspolmilitiaevts anypolmilitiavsciviliansevts"



***************************************************************************************************************
**** TABLE 1: The effect of resource rents on political violence when local councils are appointed
****
****
****

estimates clear

foreach sample in "" { 
foreach fe in  "ID_lgc stm"   {
foreach list in  anywidelayout   {

foreach price in  "brent"  {

foreach allocation in   "lgc_gsa_mo" "lgc_extra_alloc_mo" {

foreach demo in "lgc_elec_status" {  

local k=1

foreach type in "`allocation'" "(`allocation'   =c.totalindex#c.`price')" {

foreach var in ${`list'}  {
di "`type'"
**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type' if lgc_elec_status==0  `sample'  , old absorb(`fe') vce(cl $cluster) 
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {

local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","& ","",.)
local tt = "`tt'-`ss'"
}

if("`k'"=="2") {
esttab using "tables/fragment-`allocation'-`price'-`demo'-`list'-iv-`tt'.tex" , drop($controls ) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(wid N LGC ymean , labels("Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="1") {
esttab using "tables/fragment-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex" , drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****   TABLE 2: Resource rents and conflict – moderating effect of having an elected local government
****
****  
****

estimates clear

foreach sample in  ""  {
foreach fe in  "idelec stm"   {
foreach list in  anywidelayout   {

foreach price in  "brent"  {

foreach allocation in  "lgc_gsa_mo" "lgc_extra_alloc_mo" {

foreach demo in "lgc_elec_status" {  

local k=1

foreach type in "`allocation' c.`demo'#c.`allocation'  `demo'"  " `demo'  (`allocation' c.`demo'#c.`allocation'  =c.totalindex#c.`price'   c.`demo'#c.totalindex#c.`price')" {

foreach var in ${`list'}  {
di "`type'"
**COMPARE REDUCED FORM COMBINED
if("`sample'"!="" )  {
eststo: reghdfe `var' $controls `type' if `sample' , old absorb(`fe') vce(cl $cluster)
		test `allocation'   +  c.`demo'#c.`allocation'=0
	local star = ""
	if inrange(r(p),.05,.1) {
	local star = "*"
	} 
	else if inrange(r(p),.01,.05)  {
	local star = "**"
	}
	else if inrange(r(p),0,.01)  {
	local star = "***"
	}
	lincom  `allocation'   +  c.`demo'#c.`allocation'
	local roundedest = round(`r(estimate)',0.001)
	estadd local skal "`roundedest'`star'"
	local roundse = round(`r(se)',0.001)
	estadd scalar skalsd `roundse'

}
else {
eststo: reghdfe `var' $controls `type'  , old absorb(`fe') vce(cl $cluster)
}
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
		
		
		test `allocation'   +  c.`demo'#c.`allocation'=0
	local star = ""
	if inrange(r(p),.05,.1) {
	local star = "*"
	} 
	else if inrange(r(p),.01,.05)  {
	local star = "**"
	}
	else if inrange(r(p),0,.01)  {
	local star = "***"
	}
	lincom  `allocation'   +  c.`demo'#c.`allocation'
	local roundedest = round(`r(estimate)',0.001)
	estadd local skal "`roundedest'`star'"
	local roundse = round(`r(se)',0.001)
	estadd scalar skalsd `roundse'

}


local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {

local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","==0","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

if("`k'"=="2") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-iv-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide   stats(emp skal skalsd emp wid N LGC ymean , layout(@  @ (@) @ @ @)   labels("\emph{Joint Test:}" "Allocations + Elected x Allocations = 0" " " " " "Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))   starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="1") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(emp skal skalsd emp N LGC ymean , layout(@  @ (@) @ @ @)   labels("\emph{Joint Test:}" "Allocations + Elected x Allocations = 0" " " " " "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))   starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****   TABLE 4: Inclusion of Ethnic Groups Through Elections and Conflict: Estimation of effect on conflict levels
****
****
****


estimates clear
global controls =" "

foreach fe in   "ID_lgc tm"  {
*"if sampleelectionrestricted==1" "if sampleelectionrestricted2==1" "if sampleelectionrestricted3==1"
foreach sample in ""  {
foreach list in  anywidelayout   {

foreach demo in "lgc_elec_status" {
local k=1


foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `demo'   `sample' , absorb(`fe') vce(cl $cluster) old keepsingletons
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {
local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

}
esttab using "tables/fragment-leveleffectplain-lgc_excl_ethngr_share-`demo'-`list'-ols-`tt'.tex" , keep( `demo' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}


***THIS ESTIMATES THE HETEROGENOUS EFFECTS LEVEL EFFECT
estimates clear
global controls =" "

foreach fe in   "ID_lgc tm"  {

foreach sample in ""  {
foreach list in  anywidelayout   {
foreach demo in "lgc_elec_status" {
local k=1
*lgc_excl_ethnfam_share
foreach type in  lgc_excl_ethngr_share   {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `demo'  c.`demo'#c.`type'  `type' if `type'!=. `sample' , absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {
local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

}
esttab using "tables/fragment-leveleffect-`type'-`demo'-`list'-ols-`tt'.tex" , keep( `demo'  c.`demo'#c.`type'  `type') drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}
}
***************************************************************************************************************



*************************************************************************************************************** 
****   TABLE 5: Inclusion of Ethnic Groups Through Elections and Conflict: Triple Difference-in-Differences with Allocated Rents
****   TABLE A22: Continuous measure of non-aligned ethnic group share - Inclusion of Ethnic Groups Through Elections and Conflict: Triple Difference-in-Differences with Allocated Rents
****
****


estimates clear
global controls =" "
foreach fe in    "idelec tm"     {
foreach list in  anywidelayout   {

foreach allocation in  "lgc_gsa_mo"  {

local k=1
*  lgc_excl_ethnfam_share "oilproducing"  "oilgasfielddummy"
foreach type in  lgc_excl_ethngr_share  {

forvalues sample=0(1)0 { 
foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `allocation' c.`type'#c.`allocation' `type' if  lgc_elec_status ==`sample' &`type'!=., absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

esttab using "tables/fragment-sample`sample'-heterogenous-`allocation'-`type'-`list'-ols-`tt'.tex" , keep(`allocation' c.`type'#c.`allocation' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}
}


***THIS IS THE QUADROUPLE DIFFERENCE 

estimates clear
global controls =" "

foreach fe in  "idelec tm"   {
foreach list in  anywidelayout   {

foreach allocation in   "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {
local k=1
*lgc_excl_ethnfam_share
foreach type in  lgc_excl_ethngr_share   {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `demo'  `allocation' c.`demo'#c.`allocation' c.`demo'#c.`type'#c.`allocation' c.`demo'#c.`type' c.`type'#c.`allocation' `type' if `type'!=., absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
}
esttab using "tables/fragment-triplediff-`allocation'-`type'-`demo'-`list'-ols-`tt'.tex" , keep(c.`demo'#c.`type'#c.`allocation' c.`type'#c.`allocation' `allocation' c.`demo'#c.`allocation' ) order(`allocation' c.`demo'#c.`allocation'  c.`type'#c.`allocation' c.`demo'#c.`type'#c.`allocation') drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
***************************************************************************************************************
***************************************************************************************************************
****   TABLE A21: Continuous measure of non-aligned ethnic group share - Inclusion of Ethnic Groups Through Elections and Conflict: Estimation of effect on conflict levels
****
****
**** 


su lgc_excl_ethngr_share, det

gen aboveexc = lgc_excl_ethngr_share>`r(p50)' if lgc_excl_ethngr_share!=.
label variable aboveexc "Above Median Non-aligned ethnic group sh."


***THIS ESTIMATES THE PLAIN  LEVEL EFFECT
estimates clear
global controls =" "

foreach fe in   "ID_lgc tm"  {
foreach sample in ""  {
foreach list in  anywidelayout   {

foreach demo in "lgc_elec_status" {
local k=1


foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `demo'   `sample' , absorb(`fe') vce(cl $cluster) old keepsingletons
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {
local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

}
esttab using "tables/fragment-leveleffectplain-lgc_excl_ethngr_share-`demo'-`list'-ols-`tt'.tex" , keep( `demo' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}


***THIS ESTIMATES THE HETEROGENOUS EFFECTS LEVEL EFFECT
estimates clear
global controls =" "

cap drop fullint*
gen fullint = aboveexc*lgc_elec_status
label variable fullint "Elected x Above Median Non-aligned ethnic group sh."


foreach fe in   "ID_lgc tm"  {

foreach sample in ""  {
foreach list in  anywidelayout   {
foreach demo in "lgc_elec_status" {
local k=1
*lgc_excl_ethnfam_share
foreach type in  aboveexc   {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls  `demo'  fullint  aboveexc  if `type'!=. `sample' , absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {
local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

}
esttab using "tables/fragment-leveleffect-dummified-`type'-`demo'-`list'-ols-`tt'.tex" , keep( `demo' fullint ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}
}
***************************************************************************************************************


***************************************************************************************************************
***************************************************************************************************************
***************************************************************************************************************

*ALTERNATIVE TABLE 5


cap drop fullint*
gen fullint = aboveexc*lgc_gsa_mo
gen fullint2 = (1-aboveexc)*lgc_gsa_mo


label variable fullint "$\quad$  Above Median Non-aligned ethnic group sh."
label variable fullint2 "$\quad$  Below Median Non-aligned ethnic group sh."

estimates clear
global controls =" "
foreach fe in    "idelec tm"     {
foreach list in  anywidelayout   {

foreach allocation in  "lgc_gsa_mo"  {

local k=1
foreach type in  aboveexc  {

forvalues sample=0(1)0 { 
foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls fullint fullint2 if  lgc_elec_status ==`sample' &`type'!=., absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
		
		
		
		test fullint- fullint2   = 0
	local star = ""
	if inrange(r(p),.05,.1) {
	local star = "*"
	} 
	else if inrange(r(p),.01,.05)  {
	local star = "**"
	}
	else if inrange(r(p),0,.01)  {
	local star = "***"
	}
	lincom  fullint- fullint2
	local roundedest = round(`r(estimate)',0.001)
	estadd local skal "`roundedest'`star'"
	local roundse = round(`r(se)',0.001)
	estadd scalar skalsd `roundse'

}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

*esttab using "tables/fragment-dummified-sample`sample'-heterogenous-`allocation'-`type'-`list'-ols-`tt'.tex" , keep(fullint fullint2 )drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(emp skal skalsd emp N LGC ymean , layout(@  @ (@) @ @ @)   labels("\emph{Joint Test:}" "Above Median - Below Median = 0" " " " " "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))   starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
esttab using "tables/fragment-dummified-sample`sample'-heterogenous-`allocation'-`type'-`list'-ols-`tt'.tex" , keep(fullint fullint2 ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide   stats(emp skal skalsd emp N LGC ymean , layout(@  @ (@) @ @ @)   labels("\emph{Joint Test:}" "Above Median - Below Median = 0" " " " " "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))     starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}
}


***THIS IS THE QUADROUPLE DIFFERENCE 


estimates clear
global controls =" "

foreach fe in  "idelec tm"   {
foreach list in  anywidelayout   {

foreach allocation in   "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {
local k=1
*lgc_excl_ethnfam_share
foreach type in  aboveexc   {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `demo'  `allocation' c.`demo'#c.`allocation' c.`demo'#c.`type'#c.`allocation' c.`demo'#c.`type' c.`type'#c.`allocation' `type' if `type'!=., absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
}
esttab using "tables/fragment-triplediff-`allocation'-`type'-`demo'-`list'-ols-`tt'.tex" , keep(c.`demo'#c.`type'#c.`allocation' c.`type'#c.`allocation' `allocation' c.`demo'#c.`allocation' ) order(`allocation' c.`demo'#c.`allocation'  c.`type'#c.`allocation' c.`demo'#c.`type'#c.`allocation') drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}
}


cap drop fullint*
gen fullint = lgc_elec_status*aboveexc*lgc_gsa_mo
gen fullint2 = lgc_elec_status*(1-aboveexc)*lgc_gsa_mo
gen fullint3 = (1-lgc_elec_status)*aboveexc*lgc_gsa_mo
gen fullint4 = (1-lgc_elec_status)*(1-aboveexc)*lgc_gsa_mo

label variable fullint "$\quad$ Elected x Above Median Non-aligned ethnic group sh."
label variable fullint2 "$\quad$ Elected x Below Median Non-aligned ethnic group sh."
label variable fullint3 "$\quad$ Appointed x Above Median Non-aligned ethnic group sh."
label variable fullint4 "$\quad$ Appointed x Below Median Non-aligned ethnic group sh."


estimates clear
global controls =" "

foreach fe in  "idelec tm"   {
foreach list in  anywidelayout   {

foreach allocation in   "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {
local k=1
*lgc_excl_ethnfam_share
foreach type in  aboveexc   {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls  fullint fullint3 fullint2  fullint4 c.aboveexc#c.lgc_gsa_mo c.lgc_elec_status#c.aboveexc  c.lgc_elec_status#c.lgc_gsa_mo aboveexc lgc_elec_status lgc_gsa_mo if `type'!=., absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}

	test fullint - fullint3 = 0
	local star = ""
	if inrange(r(p),.05,.1) {
	local star = "*"
	} 
	else if inrange(r(p),.01,.05)  {
	local star = "**"
	}
	else if inrange(r(p),0,.01)  {
	local star = "***"
	}
	lincom  fullint - fullint3
	local roundedest = round(`r(estimate)',0.001)
	estadd local skal "`roundedest'`star'"
	local roundse = round(`r(se)',0.001)
	estadd scalar skalsd `roundse'

	test fullint2 - fullint4 = 0
	local star = ""
	if inrange(r(p),.05,.1) {
	local star = "*"
	} 
	else if inrange(r(p),.01,.05)  {
	local star = "**"
	}
	else if inrange(r(p),0,.01)  {
	local star = "***"
	}
	lincom  fullint2 - fullint4
	local roundedest = round(`r(estimate)',0.001)
	estadd local skal2 "`roundedest'`star'"
	local roundse = round(`r(se)',0.001)
	estadd scalar skalsd2 `roundse'

}



local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
}


esttab using "tables/fragment-dummified-triplediff-`allocation'-`type'-`demo'-`list'-ols-`tt'.tex" , keep(fullint fullint2 fullint3 fullint4) order(fullint  fullint3 fullint2 fullint4)  drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(emp skal skalsd emp N LGC ymean , layout(@  @ (@) @ @ @)   labels("\emph{Joint Test:}" "Elected Above Median - Appointed Above Median = 0" " " " " "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))   starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

*esttab using "tables/fragment-dummified-triplediff-`allocation'-`type'-`demo'-`list'-ols-`tt'.tex" , keep(fullint fullint2 fullint3 fullint4) order(fullint  fullint3 fullint2 fullint4)  drop($controls)  replace nolines nomtitles  fragment wrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}

***************************************************************************************************************



***************************************************************************************************************
****   TABLE A4: Decomposition of Allocations by Type and Oil Price used
****
****
****


cap drop fullint*

estimates clear

foreach controls in "brent" "crudeNGA"  {
  
foreach var in lgc_alloc_mo lgc_gsa_mo lgc_extra_alloc_mo  {

foreach fe in "const" "ID_lgc" "ID_lgc tm" { 
eststo: reghdfe `var' c.totalindex#c.`controls' `controls' totalindex_weight   ,old absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if "`fe'" == "ID_lgc" {
		estadd local lgcfe = "X"
		}	
		if "`fe'" == "ID_lgc tm" {
		estadd local lgcfe = "X"
		estadd local timefe = "X"
		}
}

}
esttab using "tables/allocation-decomposition-`controls'.tex" , keep(c.totalindex_weight#c.`controls')  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(lgcfe timefe r2 N LGC ymean , labels("LGC FE" "Time FE" "R2" "Observations" "Number of LGCs " "Mean of DV" ) fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
estimates clear

}
***************************************************************************************************************




***************************************************************************************************************
**** TABLE 


***BASIC CONFLICT RESULTS
estimates clear

foreach sample in "" { 
foreach fe in  "ID_lgc stm"   {
foreach list in  anywidelayout   {

foreach price in  "brent"  {

foreach allocation in   "lgc_extra_alloc_mo" {

foreach demo in "lgc_elec_status" {  

local k=1

foreach type in "`allocation'" "(`allocation'   =c.totalindex#c.`price')" {

foreach var in ${`list'}  {
di "`type'"
**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type' if lgc_elec_status==0  `sample'  , old absorb(`fe') vce(cl $cluster) 
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {

local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","& ","",.)
local tt = "`tt'-`ss'"
}

if("`k'"=="2") {
esttab using "tables/fragment-`allocation'-`price'-`demo'-`list'-iv-`tt'.tex" , drop($controls ) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(wid N LGC ymean , labels("Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="1") {
esttab using "tables/fragment-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex" , drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****   TABLE A3: Conflict in Oil-Producing States and Oil Fields
**** 
****
****

estimates clear
global controls =" "
foreach fe in    "idelec stm"     {
foreach list in  anywidelayout   {

foreach allocation in  "lgc_gsa_mo"  {

local k=1
*  lgc_excl_ethnfam_share "oilproducing"  "oilgasfielddummy"
foreach type in  "oilproducing"  "oilgasfielddummy" {

forvalues sample=0(1)0 { 
foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `allocation' c.`type'#c.`allocation' `type' if  lgc_elec_status ==`sample' &`type'!=., absorb(`fe') vce(cl $cluster)
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

esttab using "tables/fragment-sample`sample'-heterogenous-`allocation'-`type'-`list'-ols-`tt'.tex" , keep(`allocation' c.`type'#c.`allocation' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

estimates clear

local k= `k'+1
}
}
}
}
}
***************************************************************************************************************




***************************************************************************************************************
****   TABLE A5: Robustness: Removing election related violence from estimating sample
****
****
****


estimates clear

foreach sample in  "if sampleelectionrestricted==1" "if sampleelectionrestricted2==1" "if sampleelectionrestricted3==1" {
foreach fe in  "idelec stm"   {
foreach list in  anywidelayout   {

foreach price in  "brent"  {

foreach allocation in   "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {  

local k=1

foreach type in "`allocation' c.`demo'#c.`allocation'  `demo'"  " `demo'  (`allocation' c.`demo'#c.`allocation'  =c.totalindex#c.`price'   c.`demo'#c.totalindex#c.`price')" {

foreach var in ${`list'}  {
di "`type'"
**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type' `sample' , absorb(`fe') vce(cl $cluster) old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
		
}

local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {

local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

if("`k'"=="2") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-iv-`tt'.tex" ,keep(`allocation' c.`demo'#c.`allocation') drop($controls) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(wid N LGC ymean , labels("Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="1") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex" ,keep(`allocation' c.`demo'#c.`allocation') drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****   TABLE A6: Variance Decomposition of Local Democracy Dummy 
****  
****
****


estimates clear
eststo: reghdfe lgc_elec_status    , absorb(ID_lgc) vce(cl $cluster)
qui tab  ID_lgc
local LGC = `r(r)'
estadd local LGC = "`LGC'"

estadd local lgcfe = "X"
eststo: reghdfe lgc_elec_status    , absorb(ID_lgc tm) vce(cl $cluster)
estadd local LGC = "`LGC'"

estadd local lgcfe = "X"
estadd local timefe = "X"
eststo: reghdfe lgc_elec_status    , absorb(ID_lgc stmgov tm) vce(cl $cluster)
estadd local LGC = "`LGC'"

estadd local lgcfe = "X"
estadd local timefe = "X"
estadd local stmgov = "X"
eststo: reghdfe lgc_elec_status    , absorb(ID_lgc stm) vce(cl $cluster)
estadd local LGC = "`LGC'"

estadd local lgcfe = "X"
estadd local stm = "X"

esttab using "tables/democracy-dummy-variance-decomposition.tex" ,  nocons replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(r2 bla lgcfe timefe stmgov stm N LGC , labels("R-squared" " " "LGC FE" "Time FE" "State Governor FE" "State x Time FE" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
***************************************************************************************************************



***************************************************************************************************************
****   TABLE A7: Residual Variation to Hold Local Elections: Allocations and Conflict
****   TABLE A8: Residual Variation in Local Elections: Formula Inputs and Ethnicity
****
****


estimates clear

global varsexplore1 = "lgc_alloc_mo lgc_extra_alloc_mo alloc_agg_3m alloc_agg_6m alloc_agg_12m  events_agg3m events_agg6m events_agg12m"

global varsexplore2 = "temperature chirps totalindex_weight population_weight landmass_weight_50pof10p water_supply_spread_weight oilgasfielddummy lgc_excl_ethnfam_share lgc_excl_ethngr_share  lgc_excl_religion_share"
 

**is timing endogenous after state x time FE?

forvalues i=1(1)2 {
estimates clear

foreach var in ${varsexplore`i'} {

eststo: reghdfe lgc_elec_status `var'  , absorb(stm) vce(cl $cluster)

}

eststo: reghdfe lgc_elec_status ${varsexplore`i'}  ,old absorb(stm) vce(cl $cluster)

esttab using "tables/decomposition-varsexplore`i'-lgc_elec_status-stm.tex" , keep(${varsexplore`i'})  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(r2 N, labels("R2" "Observations" ) fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
 
}

***************************************************************************************************************




***************************************************************************************************************
****   TABLE A10: Robustness: Controlling for State-Governor Specific and Election Status specific LGA Fixed Effects
****
****
****


egen lgcelecstmgov  = group(lgc st_IDgovernor lgc_elec_status)


estimates clear

foreach fe in  "lgcstategov tm"  "lgcelecstmgov tm"   {
foreach list in  anywidelayout     {

foreach allocation in  "lgc_gsa_mo" "lgc_alloc_mo"  {

foreach demo in "lgc_elec_status" {  

foreach type in  "`allocation' if lgc_elec_status==0"   "`allocation' c.`demo'#c.`allocation'  `demo'" {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type'  , absorb(`fe') vce(cl $cluster) old keepsingletons
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

if(strpos("`type'", "==")==0) {
esttab using "tables/fragment-int-`allocation'-tm-`demo'-`list'-`tt'.tex" , drop(`demo') replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

}
if(strpos("`type'", "==")!=0) {
esttab using "tables/fragment-`allocation'-`demo'-`list'-`tt'.tex" ,  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
estimates clear
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****  TABLE A12: LGC Extraordinary Allocations and Conflict
****
****
****

estimates clear

foreach sample in  ""  {
foreach fe in  "idelec stm"   {
foreach list in  anywidelayout   {

foreach price in  "brent"  {

foreach allocation in   "lgc_extra_alloc_mo" {

foreach demo in "lgc_elec_status" {  

local k=1

foreach type in "`allocation' c.`demo'#c.`allocation'  `demo'"  " `demo'  (`allocation' c.`demo'#c.`allocation'  =c.totalindex#c.`price'   c.`demo'#c.totalindex#c.`price')" {

foreach var in ${`list'}  {
di "`type'"
**COMPARE REDUCED FORM COMBINED
if("`sample'"!="" )  {
eststo: reghdfe `var' $controls `type' if `sample' , old absorb(`fe') vce(cl $cluster)
}
else {
eststo: reghdfe `var' $controls `type'  , old absorb(`fe') vce(cl $cluster)
}
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {

local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","==0","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

if("`k'"=="2") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-iv-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(wid N LGC ymean , labels("Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="1") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****  TABLE A13: Effect of Resource rents on civil conflict: Alternative conflict data
****
****


global anyotherdatalayout = "anyevent anyfatalities anynonbokoharampolmilitia anygtdevents  anygtdnkill  anyged_events anyged_fatalities"
global levotherdatalayout = "events fatalities nonbokopoliticalmilitiaevents gtdevents  gtdnkill  ged_events ged_fatalities"

estimates clear

foreach fe in "ID_lgc tm"   {
foreach list in  anyotherdatalayout levotherdatalayout    {

foreach allocation in  "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {  

foreach type in  "`allocation' if lgc_elec_status==0"   "`allocation' c.`demo'#c.`allocation'  `demo'" {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type'  , absorb(`fe') vce(cl ID_lgc) old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
		
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

if(strpos("`type'", "==")==0) {
esttab using "tables/fragment-int-`allocation'-tm-`demo'-`list'-tm.tex" , drop(`demo') replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

}
if(strpos("`type'", "==")!=0) {
esttab using "tables/fragment-`allocation'-`demo'-`list'-tm.tex" ,  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
estimates clear
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****  TABLE A14: Effect of Resource rents on civil conflict: Different transformations of dependent variables
****
****
****

global preserveby="year"

preserve
collapse (sum) lgc_alloc_mo_RAW lgc_extra_alloc_mo lgc_gsa_mo lgc_vat_mo lgc_alloc_mo ged_events ged_fatalities  events fatalities battles battlefatalities riots riotfatalities civilianviolence civilianviolencefatalities civilianmilitiaviolence civilianmilitiaviolencefatalits civilianpoliceforceviolence civilianpoliceforceviolencftlts  militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents militaryvspolmilitiaevts polmilitiavsciviliansevts soleprotesterevts scadriots anyged_events anybattle anyriot anycivilianviolence anycivilianmilitiaviolence anycivilianpoliceforceviolence anyscadriots anysoleprotesterevts anypolmilitiavsciviliansevts anymilitaryvspolmilitiaevts anyevent anymilitaryevents anyrebelsevents anypoliticalmilitiaevents anycommunalmilitiaevents anyriotevents anyprotestevents anyprotestgdelt anynongovmilprotestgdelt protestgdelt  (mean) lgc_elec_status  lgc_popM_2006 lgc_popF_2006 lgc_pop_2006 totalindex_weight, by(ID_lgc ID_sn state $preserveby)
duplicates drop ID_lgc $preserveby, force
xtset ID_lgc  $preserveby

keep if year>=1999 & year<=2014
egen syr = group(ID_sn year)
*code years as with elected status if at least half of the time is with elected status 
replace lgc_elec_status = 0 if lgc_elec_status <=0.5 & lgc_elec_status !=.
replace lgc_elec_status = 1 if lgc_elec_status >0.5 & lgc_elec_status !=.


foreach var in $anywidelayout {

replace `var' = 1 if `var'>0 & `var' !=.

}

foreach var in events fatalities  battles battlefatalities  riots riotfatalities  civilianviolence  civilianviolencefatalities civilianmilitiaviolence civilianpoliceforceviolence  polmilitiavsciviliansevts soleprotesterevts militaryvspolmilitiaevts militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents  protestgdelt {
local lab: variable label `var' 
gen `var'pc = `var'/((lgc_pop_2006/100000))
label variable `var'pc "`lab'"
}     

foreach var in events fatalities  battles battlefatalities  riots riotfatalities  civilianviolence  civilianviolencefatalities civilianmilitiaviolence civilianpoliceforceviolence  polmilitiavsciviliansevts soleprotesterevts militaryvspolmilitiaevts militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents protestgdelt {
local lab: variable label `var' 
gen log`var'pc = log( (`var'+1)/((lgc_pop_2006/100000)))
label variable log`var'pc "`lab'"
}     


label variable lgc_elec_status "Elected"
label variable lgc_gsa_mo "LGC Gross Statutory Allocations"
label variable lgc_extra_alloc_mo "LGC Extraordinary Allocations"
label variable lgc_alloc_mo "LGC Allocations"


estimates clear
global controls =" "

*
foreach fe in "ID_lgc $preserveby"   {
foreach list in  anywidelayout  pcwidelayout levelwidelayout logpcwidelayout  {

foreach allocation in  "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {  

foreach type in  "`allocation' if lgc_elec_status==0"   "`allocation' c.`demo'#c.`allocation'  `demo'" {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type'  , absorb(`fe') vce(cl ID_lgc) old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

if(strpos("`type'", "==")==0) {
esttab using "tables/fragment-int-`allocation'-$preserveby-`demo'-`list'-ols.tex" , drop(`demo') replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats( LGC ymean , labels("Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

}
if(strpos("`type'", "==")!=0) {
esttab using "tables/fragment-`allocation'-$preserveby-`demo'-`list'-ols.tex" ,  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats( LGC ymean , labels( "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
estimates clear
}
}
}
}
}
restore
***************************************************************************************************************



***************************************************************************************************************
****  TABLE A15: Different temporal resolution of the data
****
****
****   


foreach pp in "year" "tq" "tm" { 

global preserveby = "`pp'"
preserve
keep if year>=1999 & year<=2014

collapse (sum) lgc_alloc_mo_RAW lgc_extra_alloc_mo lgc_gsa_mo lgc_vat_mo lgc_alloc_mo ged_events ged_fatalities  events fatalities battles battlefatalities riots riotfatalities civilianviolence civilianviolencefatalities civilianmilitiaviolence civilianmilitiaviolencefatalits civilianpoliceforceviolence civilianpoliceforceviolencftlts  militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents militaryvspolmilitiaevts polmilitiavsciviliansevts soleprotesterevts scadriots anyged_events anybattle anyriot anycivilianviolence anycivilianmilitiaviolence anycivilianpoliceforceviolence anyscadriots anysoleprotesterevts anypolmilitiavsciviliansevts anymilitaryvspolmilitiaevts anyevent anymilitaryevents anyrebelsevents anypoliticalmilitiaevents anycommunalmilitiaevents anyriotevents anyprotestevents anyprotestgdelt anynongovmilprotestgdelt protestgdelt  (mean) lgc_elec_status  lgc_popM_2006 lgc_popF_2006 lgc_pop_2006 totalindex_weight, by(ID_lgc ID_sn state $preserveby)
duplicates drop ID_lgc $preserveby, force
xtset ID_lgc  $preserveby

egen timefe = group(ID_sn `pp')

*code years as with elected status if at least half of the time is with elected status 
replace lgc_elec_status = 0 if lgc_elec_status <=0.5 & lgc_elec_status !=.
replace lgc_elec_status = 1 if lgc_elec_status >0.5 & lgc_elec_status !=.


foreach var in $anywidelayout {

replace `var' = 1 if `var'>0 & `var' !=.

}

foreach var in events fatalities  battles battlefatalities  riots riotfatalities  civilianviolence  civilianviolencefatalities civilianmilitiaviolence civilianpoliceforceviolence  polmilitiavsciviliansevts soleprotesterevts militaryvspolmilitiaevts militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents  protestgdelt {
local lab: variable label `var' 
gen `var'pc = `var'/((lgc_pop_2006/100000))
label variable `var'pc "`lab'"
}     

foreach var in events fatalities  battles battlefatalities  riots riotfatalities  civilianviolence  civilianviolencefatalities civilianmilitiaviolence civilianpoliceforceviolence  polmilitiavsciviliansevts soleprotesterevts militaryvspolmilitiaevts militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents protestgdelt {
local lab: variable label `var' 
gen log`var'pc = log( (`var'+1)/((lgc_pop_2006/100000)))
label variable log`var'pc "`lab'"
}     


label variable lgc_elec_status "Elected"
label variable lgc_gsa_mo "LGC Gross Statutory Allocations"
label variable lgc_extra_alloc_mo "LGC Extraordinary Allocations"
label variable lgc_alloc_mo "LGC Allocations"


estimates clear
global controls =" "

*
foreach fe in "ID_lgc timefe"   {
foreach list in  anywidelayout   {

foreach allocation in  "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {  

foreach type in   "`allocation' if `demo'==0"  "`allocation' c.`demo'#c.`allocation'  `demo'" {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type'  , absorb(`fe') vce(cl ID_lgc) old
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

if(strpos("`type'", "==")==0) {
esttab using "tables/fragment-int-`allocation'-$preserveby-`demo'-`list'-ols.tex" , drop(`demo') replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats( LGC ymean , labels("Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

}
if(strpos("`type'", "==")!=0) {
esttab using "tables/fragment-`allocation'-$preserveby-`demo'-`list'-ols.tex" ,  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats( LGC ymean , labels( "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
estimates clear
}
}
}
}
}
restore

}
***************************************************************************************************************



***************************************************************************************************************
****  TABLE A16: Effect of Resource rents on civil conflict: Alternative functional forms to account for count data
****
****
****


global preserveby="year"

preserve
collapse (sum) lgc_alloc_mo_RAW lgc_extra_alloc_mo lgc_gsa_mo lgc_vat_mo lgc_alloc_mo ged_events ged_fatalities  events fatalities battles battlefatalities riots riotfatalities civilianviolence civilianviolencefatalities civilianmilitiaviolence civilianmilitiaviolencefatalits civilianpoliceforceviolence civilianpoliceforceviolencftlts  militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents militaryvspolmilitiaevts polmilitiavsciviliansevts soleprotesterevts scadriots anyged_events anybattle anyriot anycivilianviolence anycivilianmilitiaviolence anycivilianpoliceforceviolence anyscadriots anysoleprotesterevts anypolmilitiavsciviliansevts anymilitaryvspolmilitiaevts anyevent anymilitaryevents anyrebelsevents anypoliticalmilitiaevents anycommunalmilitiaevents anyriotevents anyprotestevents anyprotestgdelt anynongovmilprotestgdelt protestgdelt  (mean) brent indexngabenchmark lgc_elec_status  lgc_popM_2006 lgc_popF_2006 lgc_pop_2006 totalindex_weight, by(ID_lgc ID_sn state $preserveby)
duplicates drop ID_lgc year, force
xtset ID_lgc  $preserveby

keep if year>=1999 & year<=2014
replace lgc_elec_status = 0 if lgc_elec_status <=0.5 & lgc_elec_status !=.
replace lgc_elec_status = 1 if lgc_elec_status >0.5 & lgc_elec_status !=.

tab year, gen(yy_)

set matsize 2000
estimates clear
global controls =" "

xtset ID_lgc year
*
foreach list in levelwidelayout   {

foreach allocation in  "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {  

local k=1

reghdfe `allocation' indexngabenchmark  , absorb(ID_lgc year,savefe) old

predict fit_`allocation' , xbd  

label variable fit_`allocation' "LGC Allocations"
label variable `demo' "Elected"

gen lev =  fit_`allocation'
gen interact = `demo' * fit_`allocation'
gen lev2 = `demo'

local lab: variable label fit_`allocation'
local lab2: variable label `demo'

label variable lev "`lab'"
label variable interact "`lab2' x  `lab'"
label variable lev2 "`lab2'"

foreach control in "lev interact lev2"  "lev if `demo'==0"  {

foreach regtype in reghdfe ppmlhdfe xtnbreg  {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
if("`regtype'"== "xtnbreg")  {
eststo: `regtype' `var' yy_*  `control'  , fe 
}
if("`regtype'"== "ppmlhdfe")  {
eststo: `regtype' `var'  `control'  , absorb(ID_lgc year) vce(cl ID_lgc)
}	
if("`regtype'"== "reghdfe")  {
eststo: `regtype' `var' `control'  , absorb(ID_lgc year) vce(cl ID_lgc)
}	
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		
		
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

if(strpos("`control'", "==")==0) {
esttab using "tables/fragment-int-`regtype'-`list'-`allocation'.tex" , drop(lev2) keep(lev interact)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
if(strpos("`control'", "==")!=0) {
esttab using "tables/fragment-`regtype'-`list'-`allocation'.tex" ,   keep(lev) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
estimates clear

local k= `k'+1
}
}
drop lev interact lev2

}
}
}

restore
***************************************************************************************************************



***************************************************************************************************************
****  TABLE A17: Robustness: Controlling flexibly for formula inputs
****   
****  
****

estimates clear
global controls =" "

foreach fe in   "idelec stm" {
foreach list in  anywidelayout   {

foreach price in  "brent" {

foreach fecontrol in  qq_population_wei_tm  qq_landmass_weigh_tm qq_combinedsocio_tm  qq_reveffort_weig_tm qq_hospital_beds__tm qq_primary_enroll_tm qq_water_supply_s_tm qq_totalindex_wei_tm {

foreach allocation in  "lgc_gsa_mo"  {

foreach demo in "lgc_elec_status" {  

local k=1

foreach type in   "`allocation' if `demo'==0" "`allocation' c.`demo'#c.`allocation' `demo'"  {

foreach var in ${`list'}  {

**COMPARE REDUCED FORM COMBINED
eststo: reghdfe `var' $controls `type'  , absorb(`fe' `fecontrol') vce(cl $cluster) 
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)


if(strpos("`type'", "==")==0) {
esttab using "tables/fragment-rob`fecontrol'-int-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex"  ,    replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide   stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
if(strpos("`type'", "==")!=0) {
esttab using "tables/fragment-rob`fecontrol'-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex"  ,  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide   stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}
local k= `k'+1



estimates clear

}
}
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****  FIGURE A2: Conventional test for non-linearities
****
****
**** 


matrix hrid = J(120 ,6, 0)
global iter =1
gen sample1 = lgc_elec_status ==0 & anycivilianviolence !=.
gen sample2 = lgc_elec_status ==1 & anycivilianviolence !=.

local varcount = 1
foreach var in $anywidelayout {
forvalues ss=1(1)2 { 

foreach vv in `var'  lgc_alloc_mo {

reghdfe `vv'  if sample`ss'==1,  absorb(ID_lgc tm) residuals(__`vv')  

}

xtile nq_lgc_alloc_mo= __lgc_alloc_mo,nq(5)
tab nq_lgc_alloc_mo, gen(nq_)


reg __`var' nq_1 nq_2 nq_3 nq_4 nq_5, nocons  vce(cl ID_lgc )

forvalues i=1(1)5 {

  lincom  nq_`i' 
  matrix hrid[$iter,3] = `r(estimate)'
  matrix hrid[$iter,2] = `r(estimate)' - 1.65 * `r(se)'
  matrix hrid[$iter,4] = `r(estimate)'+ 1.65* `r(se)'
  matrix hrid[$iter, 5] = `i'
  matrix hrid[$iter, 6] = `varcount'
  matrix hrid[$iter, 1] = `ss'
  global iter=$iter+1
  }


drop nq_* __* 
}
local  varcount = `varcount'+ 1
}

cap drop hrid*
svmat hrid
local varcount = 1
foreach var in $anywidelayout { 
su hrid1
sort hrid5

su hrid2 if hrid6==`varcount'
local miny = `r(min)'
su hrid4 if hrid6==`varcount'
local maxy = `r(max)'

twoway (connected hrid3 hrid5 if hrid1==1 & hrid6==`varcount' ,ytitle("Coefficient estimate", axis(1))) (rcap hrid2 hrid4 hrid5  if hrid1==1 & hrid6==`varcount', yline(0,lcolor(red)))  , scheme(s1mono) legend(off)  xtitle("Quintile", size(4))  yscale(range(`miny' `maxy') ) ylabel(`miny'[0.01]`maxy')
graph export  figures/quintile-effect-`var'-pre.png, replace width(512)
twoway (connected hrid3 hrid5 if hrid1==2 & hrid6==`varcount' ,ytitle("Coefficient estimate", axis(1))) (rcap hrid2 hrid4 hrid5  if hrid1==2 & hrid6==`varcount', yline(0,lcolor(red)))  , scheme(s1mono) legend(off)  xtitle("Quintile", size(4))  yscale(range(`miny' `maxy') )  ylabel(`miny'[0.01]`maxy')
graph export  figures/quintile-effect-`var'-post.png, replace width(512)


local  varcount = `varcount'+ 1


}
drop hrid*
***************************************************************************************************************



***************************************************************************************************************
****  FIGURE A1: Conflict around election months
****
****


matrix hrid = J(30 ,6, 0)
foreach window of numlist 5    {
foreach case in timetoelectime    {

tab `case' if `case'>=-`window' & `case'<=`window', gen(ttt_)
local rows = "`r(r)'"

local omit = `rows'-`window'
local omitl1 = `omit'-1
local omitp1 = `omit'+1

foreach var in anyevent $anywidelayout {

foreach fe in  tm  {

reghdfe `var', absorb(ID_lgc tm) res(res_`var')


reghdfe res_`var' o.ttt_1 ttt_2-ttt_`rows' ,  absorb(ID_lgc tm) vce(cl ID_sn )

drop res_`var'
global iter  = 1
forvalues i=1(1)`rows' {

  lincom  ttt_`i' 
  matrix hrid[$iter,3] = `r(estimate)'
  matrix hrid[$iter,2] = `r(estimate)' - 1.65 * `r(se)'
  matrix hrid[$iter,4] = `r(estimate)'+ 1.65* `r(se)'
  su `case' if  ttt_`i'== 1
  matrix hrid[$iter, 1] = `r(mean)'
  matrix hrid[$iter, 5] = `r(N)'

  global iter=$iter+1
  }

  
cap drop hrid*
  svmat hrid
sort hrid1

su hrid2
local miny = `r(min)'
su hrid4
local maxy = `r(max)'
drop if hrid1==0 & hrid2==0 & hrid3==0 & hrid5==0

 
su hrid1
sort hrid1
twoway (connected hrid3 hrid1 ,ytitle("Coefficient estimate", axis(1))) (rcap hrid2 hrid4 hrid1 , yline(0,lcolor(red)))  , scheme(s1color) legend(off) xtitle("Months to election", size(4)) xscale(range(`r(min)' `r(max)')) xlabel(`r(min)'[1]`r(max)') yscale(range(`miny' `maxy') )
graph export  figures/event-nearelection-`var'-`fe'-`case'-`window'.png, replace width(512)
drop hrid*

}

}
drop ttt*
}
}
***************************************************************************************************************



****   RANDOMISATION INFERENCE

***************************************************************************************************************
****  FIGURE A4: Permutation test on gross statutory allocations and election status
****   
****
****

capture drop _merge
cap drop hrid*
duplicates drop ID_lgc year month, force
merge 1:1 ID_2 year month using "data files/PERMUTATION.dta"

matrix hrid = J(100 ,6, 0)
global iter=1
forvalues i=1(1)100 {
di "`i'"

reghdfe anycivilianviolence   perm_lgc_elec_status`i'  c.perm_lgc_elec_status`i'#c.lgc_gsa_mo  lgc_gsa_mo ,  absorb(ID_lgc stm) vce(cl ID_lgc tm)
  matrix hrid[$iter,1] = `i'
lincom c.perm_lgc_elec_status`i'#c.lgc_gsa_mo 
  matrix hrid[$iter,2] = `r(estimate)' 
  
lincom  perm_lgc_elec_status`i'
  matrix hrid[$iter,3] = `r(estimate)' 
    
lincom  lgc_gsa_mo
  matrix hrid[$iter,4] = `r(estimate)' 
    
    
lincom  lgc_gsa_mo+c.perm_lgc_elec_status`i'#c.lgc_gsa_mo 
  matrix hrid[$iter,5] = `r(estimate)' 
    
global iter = $iter+1
}

cap drop hrid*
svmat hrid
reghdfe anycivilianviolence   lgc_elec_status  c.lgc_elec_status#c.lgc_gsa_mo  lgc_gsa_mo ,  absorb(ID_lgc stm) vce(cl ID_lgc tm)
lincom c.lgc_elec_status`i'#c.lgc_gsa_mo


twoway kdensity hrid2,  xline(`r(estimate)', lcol(red)) scheme(s1mono) range(-.2 .2)
graph export  "figures/permutation-test-interact.pdf" , replace

lincom c.lgc_gsa_mo
twoway kdensity hrid4,  xline(`r(estimate)', lcol(red)) scheme(s1mono) range(-.2 .2)
graph export  "figures/permutation-test-level.pdf" , replace

reghdfe anycivilianviolence   lgc_elec_status  c.lgc_elec_status#c.lgc_gsa_mo  lgc_gsa_mo ,  absorb(ID_lgc stm) vce(cl ID_lgc tm)
lincom c.lgc_elec_status#c.lgc_gsa_mo
local lll = `r(estimate)'
lincom lgc_gsa_mo
twoway kdensity hrid2,  xline(`lll', lcol(maroon) lw(1.25)) xline(`r(estimate)', lcol(navy) lw(1.25) lp(-)) scheme(s1mono) range(-.08 .2) xtitle("Distribution of point estimates")
graph export  "figures/permutation-test-interact-both.pdf" , replace

***************************************************************************************************************



***************************************************************************************************************
****   FIGURE A5: Permutation test on election status dummy across 37 states
****
****
****

capture drop _merge
cap drop hrid*
duplicates drop ID_lgc year month, force
merge m:1 ID_1 year month using "data files/PERMUTATION.STATELEVEL.dta"

matrix hrid = J(100 ,6, 0)
global iter=1
forvalues i=1(1)100 {
di "`i'"

reghdfe anycivilianviolence   perm_nastate_lgc_elec`i'  c.perm_nastate_lgc_elec`i'#c.lgc_gsa_mo  lgc_gsa_mo ,  absorb(ID_lgc tm)  
  matrix hrid[$iter,1] = `i'
lincom c.perm_nastate_lgc_elec`i'#c.lgc_gsa_mo 
  matrix hrid[$iter,2] = `r(estimate)' 
  
lincom  perm_nastate_lgc_elec`i'
  matrix hrid[$iter,3] = `r(estimate)' 
    
lincom  lgc_gsa_mo
  matrix hrid[$iter,4] = `r(estimate)' 
    
    
lincom  lgc_gsa_mo+c.perm_nastate_lgc_elec`i'#c.lgc_gsa_mo 
  matrix hrid[$iter,5] = `r(estimate)' 
    
global iter = $iter+1
}


cap drop hrid*
svmat hrid

reghdfe anycivilianviolence   naloclgcelec   c.naloclgcelec#c.lgc_gsa_mo  lgc_gsa_mo ,  absorb(ID_lgc tm) vce(cl ID_lgc tm)
lincom c.naloclgcelec#c.lgc_gsa_mo
local lll = `r(estimate)'
lincom lgc_gsa_mo
twoway kdensity hrid2,  xline(`lll', lcol(maroon) lw(1.25)) xline(`r(estimate)', lcol(navy) lw(1.25) lp(-)) scheme(s1mono) range(-.08 .2) xtitle("Distribution of point estimates")
graph export  "figures/permutation-statelevel-test.pdf" , replace



***************************************************************************************************************



***************************************************************************************************************
****   ALTERNATIVE CLUSTERING
****
****
****

egen stmgovlgc = group(stmgov lgc_elec_status)



foreach cluster in "ID_lgc tm" "stmgov" "stmgov" "stmgov tm" { 
 estimates clear

foreach sample in  ""  {
foreach fe in  "idelec stm"   {
foreach list in  anywidelayout   {

foreach price in  "brent"  {

foreach allocation in   "lgc_gsa_mo" {

foreach demo in "lgc_elec_status" {  

local k=2

*  "`allocation' c.`demo'#c.`allocation'  `demo'" 
foreach type in  " `demo'  (`allocation' c.`demo'#c.`allocation'  =c.totalindex#c.`price'   c.`demo'#c.totalindex#c.`price')" {

foreach var in ${`list'}  {
di "`type'"
**COMPARE REDUCED FORM COMBINED
if("`sample'"!="" )  {
eststo: reghdfe `var' $controls `type' if `sample' , old absorb(`fe') vce(cl `cluster')
}
else {
eststo: reghdfe `var' $controls `type'  , old absorb(`fe') vce(cl `cluster')
}
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}


local cc = subinstr("`cluster'"," ","-",.)
local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {

local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","==0","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

if("`k'"=="2") {
esttab using "tables/fragment-int-cluster`cc'-`allocation'-`price'-`demo'-`list'-iv-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(wid N LGC ymean , labels("Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="1") {
esttab using "tables/fragment-int-cluster`cc'-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}
}
}
}
}
***************************************************************************************************************



***************************************************************************************************************
****   FIGURE A3: Leave-one-out validation: Dropping each state in turn
****
****
****

*foreach depvar in $anywidelayout  {

foreach depvar in anyevents   { 
preserve


reghdfe  `depvar'  lgc_elec_status (lgc_gsa_mo c.lgc_elec_status#c.lgc_gsa_mo  =c.totalindex#c.brent   c.lgc_elec_status#c.totalindex#c.brent)      , old absorb(ID_lgc stm) vce(cl $cluster)

*reghdfe  `depvar'    lgc_gsa_mo c.lgc_elec_status#c.lgc_gsa_mo     , old absorb(idelec stm) vce(cl $cluster)

lincom lgc_gsa_mo
global maineffect = `r(estimate)'
lincom c.lgc_elec_status#c.lgc_gsa_mo
global inteffect = `r(estimate)'

tab state if e(sample), gen(ss_)

encode state, gen(s)

local states = `r(r)'
matrix hrid = J(`states',7,.)

estimates clear
forvalues i=1(1)`states' { 

qui reghdfe  `depvar'  lgc_elec_status  (lgc_gsa_mo c.lgc_elec_status#c.lgc_gsa_mo  =c.totalindex#c.brent   c.lgc_elec_status#c.totalindex#c.brent)  if ss_`i'!=1    , old absorb(ID_lgc stm) vce(cl $cluster)
*qui reghdfe  `depvar'    lgc_gsa_mo c.lgc_elec_status#c.lgc_gsa_mo   if ss_`i'!=1    , old absorb(ID_lgc stm) vce(cl $cluster)

su s if ss_`i'==1
  matrix hrid[`i',1] = `r(mean)'
lincom lgc_gsa_mo
  matrix hrid[`i',2] = `r(estimate)'
  matrix hrid[`i',3] = `r(se)'
  matrix hrid[`i',4] = `r(estimate)'/`r(se)'
lincom c.lgc_elec_status#c.lgc_gsa_mo
  matrix hrid[`i',5] = `r(estimate)'
  matrix hrid[`i',6] = `r(se)'
  matrix hrid[`i',7] = `r(estimate)'/`r(se)'

}


cap drop hrid*
svmat hrid

label values hrid1 s

label variable hrid2 "Distribution of estimates"
su hrid2, det

local round = round( `r(max)',.01)+.01

graph box hrid2, marker(1, msymbol(o) mlabel(hrid1)) scheme(s1mono) yscale(range(0 `round'))  ylabel(0[0.02]`round')  yli($maineffect, lcol(maroon) lw(1))
*ylabel(`r(min)'[.01]`r(max)')   

graph export "figures/leave-one-out-iv-main-`depvar'.pdf" , replace  
graph save "figures/leave-one-out-iv-main-`depvar'.gph" , replace  


label variable hrid5 "Distribution of estimates"
su hrid5, det

local round = round( `r(max)',.01)-.01

graph box hrid5, marker(1, msymbol(o) mlabel(hrid1)) scheme(s1mono) yscale(range(`round' 0))  ylabel(`round'[0.02]0) yli($inteffect, lcol(maroon) lw(1))
*ylabel(`r(min)'[.01]`r(max)')   

graph export "figures/leave-one-out-iv-int-`depvar'.pdf" , replace  
graph save "figures/leave-one-out-iv-int-`depvar'.gph" , replace  

restore
}




*foreach depvar in $anywidelayout  {
*anypoliticalmilitiaevents
foreach depvar in anyevents  { 
preserve


reghdfe  `depvar'    lgc_gsa_mo c.lgc_elec_status#c.lgc_gsa_mo   lgc_elec_status  , old absorb(ID_lgc stm) vce(cl $cluster)

lincom lgc_gsa_mo
global maineffect = `r(estimate)'
lincom c.lgc_elec_status#c.lgc_gsa_mo
global inteffect = `r(estimate)'

tab state if e(sample), gen(ss_)

encode state, gen(s)

local states = `r(r)'
matrix hrid = J(`states',7,.)

estimates clear
forvalues i=1(1)`states' { 

qui reghdfe  `depvar'    lgc_gsa_mo c.lgc_elec_status#c.lgc_gsa_mo  lgc_elec_status  if ss_`i'!=1    , old absorb(ID_lgc stm) vce(cl $cluster)

su s if ss_`i'==1
  matrix hrid[`i',1] = `r(mean)'
lincom lgc_gsa_mo
  matrix hrid[`i',2] = `r(estimate)'
  matrix hrid[`i',3] = `r(se)'
  matrix hrid[`i',4] = `r(estimate)'/`r(se)'
lincom c.lgc_elec_status#c.lgc_gsa_mo
  matrix hrid[`i',5] = `r(estimate)'
  matrix hrid[`i',6] = `r(se)'
  matrix hrid[`i',7] = `r(estimate)'/`r(se)'

}


cap drop hrid*
svmat hrid

label values hrid1 s

label variable hrid2 "Distribution of estimates"
su hrid2, det

local round = round( `r(max)',.01)+.01

graph box hrid2, marker(1, msymbol(o) mlabel(hrid1)) scheme(s1mono) yscale(range(0 `round'))  ylabel(0[0.02]`round')  yli($maineffect, lcol(maroon) lw(1))
*ylabel(`r(min)'[.01]`r(max)')   

graph export "figures/leave-one-out-main-`depvar'.pdf" , replace  


label variable hrid5 "Distribution of estimates"
su hrid5, det

local round = round( `r(max)',.01)-.01

graph box hrid5, marker(1, msymbol(o) mlabel(hrid1)) scheme(s1mono) yscale(range(`round' 0))  ylabel(`round'[0.02]0) yli($inteffect, lcol(maroon) lw(1))
*ylabel(`r(min)'[.01]`r(max)')   

graph export "figures/leave-one-out-int-`depvar'.pdf" , replace  

restore
}


***************************************************************************************************************



***************************************************************************************************************
****   TABLE A9: Sensitivity of results to data where there may have been disruption to revenue sharing
****
****
****

**WITHHOLDING TYPE1 and TYPE 2
local intvar = "lgc_gsa_mo"
local depvar = "anypoliticalmilitiaevents"

replace d_st_mo_alloc_wh =  d_st_mo_alloc_wh==1 | ded_alloc_all==1 if ded_alloc_all!=. & d_st_mo_alloc_wh!=.

egen ded_alloc_all_yr = max(ded_alloc_all), by(ID_lgc year)

replace d_st_yr_alloc_wh = d_st_yr_alloc_wh ==1 | ded_alloc_all_yr==1 if ded_alloc_all_yr!=. &d_st_yr_alloc_wh!=.

foreach var in   d_st_yr_alloc_newlga d_st_yr_er_plateau d_st_yr_er_ekiti d_st_yr_alloc_boko d_st_yr_alloc_wh ded_alloc_all {

egen max`var' = max(`var'), by(ID_lgc)

}


foreach pre in "" "max" {
estimates clear
foreach var in  d_st_yr_alloc_newlga d_st_yr_er_plateau d_st_yr_er_ekiti d_st_yr_alloc_boko ded_alloc_all d_st_yr_alloc_wh { 

eststo: reghdfe `depvar' c.lgc_elec_status#c.`intvar'  `intvar'   lgc_elec_status if `pre'`var' ==0, absorb(ID_lgc stm) vce(cl $cluster) old keepsingletons
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)

esttab using "tables/fragment-rob-withholding`pre'-int-`depvar'-`intvar'.tex"  ,   keep( c.lgc_elec_status#c.`intvar'  `intvar' )  order(`intvar' c.lgc_elec_status#c.`intvar') replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide   stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}


estimates clear
foreach var in  d_st_mo_alloc_newlga d_st_mo_er_plateau d_st_mo_er_ekiti d_st_mo_alloc_boko ded_alloc_all d_st_mo_alloc_wh { 

eststo: reghdfe `depvar' c.lgc_elec_status#c.`intvar'  `intvar'   lgc_elec_status if `var' ==0, absorb(ID_lgc stm) vce(cl $cluster) old keepsingletons
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)
local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)


esttab using "tables/fragment-rob-withholding-month-int-`depvar'-`intvar'.tex"  ,   keep( c.lgc_elec_status#c.`intvar' `intvar') order(`intvar' c.lgc_elec_status#c.`intvar') replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide   stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))

***************************************************************************************************************



***************************************************************************************************************
****  TABLE A11: Dropping all data from states that may have been impacted by the Boko Haram insurgency: Resource rents and conflict – ****   moderating effect of having an elected local government
****
****
**** FOR REFEREE

gen bokoharamstatebroad= state=="YOBE" | state=="BORNO" | state=="ADAMAWA" | state=="NIGER" | state=="PLATEAU"

gen bokoharamstate= state=="YOBE" | state=="BORNO" | state=="ADAMAWA" 

gen beforebokoharam  = year<=2009

estimates clear

foreach sample in   "bokoharamstate==0" "bokoharamstatebroad==0" "beforebokoharam==1" {
foreach fe in  "ID_lgc stm"   {
foreach list in  anywidelayout   {

foreach price in  "brent"  {

foreach allocation in  "lgc_alloc_mo"  {

foreach demo in "lgc_elec_status" {  

local k=1
foreach type in "`allocation' c.`demo'#c.`allocation'  `demo'"  " `demo'  (`allocation' c.`demo'#c.`allocation'  =c.totalindex#c.`price'   c.`demo'#c.totalindex#c.`price')" {
foreach var in ${`list'}  {
di "`type'"
**COMPARE REDUCED FORM COMBINED
if("`sample'"!="" )  {
eststo: reghdfe `var' $controls `type' if `sample' , old absorb(`fe') vce(cl $cluster) keepsingletons
}
else {
eststo: reghdfe `var' $controls `type'  , old absorb(`fe') vce(cl $cluster) keepsingletons
}
		estadd ysumm
		qui tab ID_lgc if e(sample)
		estadd local LGC = `r(r)'
		if("`e(widstat)'"!="") {
		estadd local wid = string(round(`e(widstat)', 0.01), "%12.4g")
		}
}

local tt = subinstr("`fe'"," ","-",.)

local tt = subinstr("`tt'","(","",.)
local tt = subinstr("`tt'",")","",.)
local tt = subinstr("`tt'","=","-",.)
if("`sample'" !="") {

local ss = subinstr("`sample'","==1","",.)
local ss = subinstr("`ss'","==0","",.)
local ss = subinstr("`ss'","if ","",.)
local tt = "`tt'-`ss'"
}

if("`k'"=="2") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-iv-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls) replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(wid N LGC ymean , labels("Kleibergen-Paap weak IV" "Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
} 
else if("`k'"=="1") {
esttab using "tables/fragment-int-`allocation'-`price'-`demo'-`list'-ols-`tt'.tex" , keep(`allocation' c.`demo'#c.`allocation' ) drop($controls)  replace nolines nomtitles  fragment nowrap   label nonumbers nodepvars noobs eqlabels("") style(tex) wide  stats(N LGC ymean , labels("Observations" "Number of LGCs " "Mean of DV") fmt(%9.3g))  starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par))
}

estimates clear

local k= `k'+1
}
}
}
}
}
}
}
***************************************************************************************************************



